package com.zhang.passer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhang.passer.domain.ImageDo;
import com.zhang.passer.domain.UserSafe;
import com.zhang.passer.domain.YouJiDo;
import com.zhang.passer.mapper.ImageMapper;
import com.zhang.passer.mapper.YouJiMapper;
import com.zhang.passer.service.YoujiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("youji")
public class YoujiServiceImpl implements YoujiService {

    @Autowired
    private YouJiMapper youJiMapper;

    @Autowired
    ImageMapper imageMapper;

    @Override
    public List<YouJiDo> queryAll(YouJiDo YouJiDo) {
        QueryWrapper<YouJiDo> YouJiDoQueryWrapper = new QueryWrapper<>(YouJiDo);
        return youJiMapper.selectList(YouJiDoQueryWrapper);
    }

    @Override
    public List<YouJiDo> queryNew7() {
        List<YouJiDo> youji = youJiMapper.selectList(new QueryWrapper<YouJiDo>().last("order by time desc limit 7"));
        for (YouJiDo youJiDo : youji) {
            ArrayList<ImageDo> images = new ArrayList<>();
            images.add(imageMapper.selectOne(new QueryWrapper<>(new ImageDo().setYid(youJiDo.getYid())).last("order by sid desc limit 1")));
            youJiDo.setImages(images);
            // TODO: 2021/11/12 从数据库获得用户信息
            youJiDo.setUser(new UserSafe().setUid(1).setNickname("小张"));
        }
        return youji;
    }


    @Override
    public Page<YouJiDo> selectPage(int currentPage, int pageSize, QueryWrapper<YouJiDo> queryWrapper) {
        //封装pageBean(Mybatis_plus中简单的分页模型)
        Page<YouJiDo> page = new Page<>(currentPage,pageSize,true);
        //selectPage直接将结果返回给Page对象
        youJiMapper.selectPage(page, queryWrapper);
        //遍历所有景点填充景点图片列表、用户信息
        List<YouJiDo> records = page.getRecords();
        for (YouJiDo record : records) {
            Map<String, Object> map = new HashMap<>();
            map.put("yid",record.getYid());

            List<ImageDo> imageDos = imageMapper.selectByMap(map);

            record.setImages(imageDos);

            // TODO: 2021/11/9  找不到对应的图片,设置图片为找不到的图片
            if (imageDos.isEmpty()){
                List<ImageDo> ImageNull = new ArrayList<>();
                ImageNull.add(new ImageDo().setSrc("#").setTitle("图片找不到").setHref("#"));
                record.setImages(ImageNull);
            }

            // TODO: 2021/11/9 从数据库获得用户信息
            record.setUser(new UserSafe().setUid(1).setNickname("小张"));
        }

        return page;
    }

    @Override
    public YouJiDo queryDetail(int id) {
        //设置查询参数
        QueryWrapper<YouJiDo> queryWrapper = new QueryWrapper<>(new YouJiDo().setYid(id));
        YouJiDo YouJiDo = youJiMapper.selectOne(queryWrapper);
        YouJiDo.setImages(imageMapper.selectList(new QueryWrapper<>(new ImageDo().setYid(id))));
        return YouJiDo;
    }


}
